﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bot.Domain.Report
{
    public class BaseReportInputDto
    {
        [JsonIgnore]
        public int SiteId { get; set; }
        [Required]
        [JsonConverter(typeof(StringEnumConverter))]
        public EnumFilterType FilterType { get; set; }
        public string FilterValue { get; set; }
        public string Channel { get; set; } = "";
        public DateTime? TimeFrom { get; set; }
        public DateTime? TimeTo { get; set; }
        //public string Timezone { get; set; } = "+00:00";

        [JsonConverter(typeof(StringEnumConverter))]
        public virtual DisplayType? DisplayBy { get; set; }
        [JsonConverter(typeof(StringEnumConverter))]
        [Required]
        public EnumDateRangeType? DateRange { get; set; }
        [JsonIgnore]
        public string DateFormat { get; set; }
        [Required]
        [JsonConverter(typeof(StringEnumConverter))]
        public EnumDimensionType DimensionType { get; set; }

        [JsonIgnore]
        public DateTime DtTimeFrom { get; set; }
        [JsonIgnore]
        public DateTime DtTimeTo { get; set; }
        public string SummaryChannel { get; set; } = "livechat";

        //public bool TryTimeToUtc()
        //{
        //    Timezone = Timezone.Replace("+", "");
        //    TimeSpan  Offset;
        //    if (!TimeSpan.TryParse(Timezone, out Offset))
        //        return false;
        //    DateTimeOffset timeOffset = new DateTimeOffset(TimeFrom, Offset);
        //    TimeFrom = timeOffset.UtcDateTime;
        //    timeOffset = new DateTimeOffset(TimeTo, Offset);
        //    TimeTo= timeOffset.UtcDateTime;

        //    return true;
        //}

        public virtual int CheckValidation()
        {
            if (!DisplayBy.HasValue)
            {
                return 55218;
            }

            if (!DateRange.HasValue)
            {
                return 55211;
            }

            if (DateRange == EnumDateRangeType.Custom)
            {
                if(!TimeTo.HasValue ||
                    !TimeFrom.HasValue)
                {
                    return 55213;
                }

                //TimeTo = TimeTo.AddMilliseconds(-1);
                if (TimeTo < TimeFrom)
                    return 55202;

                TimeSpan timeSpan = TimeTo.Value - TimeFrom.Value;
                if (timeSpan.Days > 365)
                    return 55203;
            }

            //Timezone = Timezone.TrimStart('+');
            //TimeSpan offset;
            //if (!TimeSpan.TryParse(Timezone, out offset))
            //    return 55201;
            //if (offset.TotalMinutes > 840 || offset.TotalMinutes < -720)
            //    return 55209;

            //offset = TimeSpan.Parse(Timezone.TrimStart('+'));

            if (DimensionType != EnumDimensionType.ByTime /*&& DimensionType != EnumDimensionType.AllChannel && DimensionType != EnumDimensionType.LivechatChannel && DimensionType != EnumDimensionType.FacebookChannel && DimensionType != EnumDimensionType.TwitterChannel*/)
                return 55206;
            if (!(FilterType == EnumFilterType.Bot || FilterType == EnumFilterType.Site || FilterType == EnumFilterType.Campaign || FilterType == EnumFilterType.channelAccount || FilterType == EnumFilterType.chatFilterType))
                return 55205;
            if (/*DimensionType == EnumDimensionType.FacebookChannel*/ (!string.IsNullOrWhiteSpace(Channel) && Channel != "All Channels") && (FilterType != EnumFilterType.chatFilterType && FilterType != EnumFilterType.Campaign && FilterType != EnumFilterType.channelAccount && FilterType!= EnumFilterType.Bot&& FilterType!= EnumFilterType.Site))
                return 55205;
            //if (DimensionType == EnumDimensionType.TwitterChannel &&( FilterType != EnumFilterType.channelAccount && FilterType != EnumFilterType.Bot && FilterType != EnumFilterType.Site))
            //    return 55205;
            //if(DimensionType== EnumDimensionType.LivechatChannel&&(FilterType!=  EnumFilterType.Bot&& FilterType!= EnumFilterType.Site&& FilterType!= EnumFilterType.Campaign))
            //    return 55205;
            if (/*DimensionType == EnumDimensionType.AllChannel*/ (!string.IsNullOrWhiteSpace(Channel)&&Channel == "All Channels") && (FilterType != EnumFilterType.Bot && FilterType != EnumFilterType.Site))
                return 55205;

            var strDateRangeArr = System.Enum.GetNames(typeof(EnumDateRangeType));
            var strDisplayTypeArr = System.Enum.GetNames(typeof(DisplayType));

            if (!strDateRangeArr.Contains(DateRange.ToString()))
            {
                return 55212;
            }
            if (!strDisplayTypeArr.Contains(DisplayBy.ToString()))
            {
                return 55219;
            }

            return -1;
        }



    }
}
